package com.example.peibei.ui.activity;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.dingtao.common.bean.RefreshResult;
import com.dingtao.common.bean.User;
import com.dingtao.common.core.DataCall;
import com.dingtao.common.core.WDActivity;
import com.dingtao.common.core.exception.ApiException;
import com.dingtao.common.util.SPUtils;
import com.dingtao.common.util.UIUtils;
import com.example.peibei.R;
import com.example.peibei.base.SpConstant;
import com.example.peibei.service.presenter.FollowUserPresenter;
import com.example.peibei.service.presenter.GetOtherUserInfoPresenter;
import com.example.peibei.service.presenter.OperateBLPresenter;
import com.example.peibei.ui.fragment.MyDynamicFragment;
import com.example.peibei.ui.fragment.MySendBillFragment;
import com.example.peibei.ui.utils.LevelImage;
import com.example.peibei.ui.weight.CenterDialog;
import com.flyco.tablayout.SlidingTabLayout;
import com.linxiao.framework.dialog.BottomDialog;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

public class PersonalInfoActivity extends WDActivity {
    @BindView(R.id.slide_tablayout)
    SlidingTabLayout tabLayout;
    @BindView(R.id.viewpager)
    ViewPager viewpager;
    @BindView(R.id.iv_head)
    ImageView iv_head;
    @BindView(R.id.tv_nickname)
    TextView tv_nickname;
    @BindView(R.id.tv_id)
    TextView tv_id;
    @BindView(R.id.tv_address)
    TextView tv_address;
    @BindView(R.id.tv_age)
    TextView tv_age;
    @BindView(R.id.tv_height)
    TextView tv_height;
    @BindView(R.id.iv_level)
    ImageView iv_level;
    @BindView(R.id.tv_favorites)
    TextView tv_favorites;
    @BindView(R.id.tv_follow)
    TextView tv_follow;
    @BindView(R.id.tv_sm)
    TextView tv_sm;
    @BindView(R.id.tv_phone)
    TextView tv_phone;
    @BindView(R.id.tv_alipay)
    TextView tv_alipay;
    @BindView(R.id.tv_wx)
    TextView tv_wx;
    @BindView(R.id.sign)
    TextView sign;
    @BindView(R.id.ll_no_follow)
    LinearLayout ll_no_follow;
    @BindView(R.id.ll_followed)
    LinearLayout ll_followed;
    @BindView(R.id.ll_phone)
    LinearLayout ll_phone;
    @BindView(R.id.ll_sm)
    LinearLayout ll_sm;
    @BindView(R.id.iv_sm)
    ImageView iv_sm;
    @BindView(R.id.iv_phone)
    ImageView iv_phone;
    @BindView(R.id.iv_other)
    ImageView iv_other;
    @BindView(R.id.ll_edit)
    LinearLayout ll_edit;

    private String[] title = {"发单信息", "动态"};
    private ArrayList<Fragment> fragments = new ArrayList<>();
    private GetOtherUserInfoPresenter getOtherUserInfoPresenter;
    private FollowUserPresenter followUserPresenter;
    private OperateBLPresenter operateBLPresenter;
    private SPUtils spUtils;
    private String token;
    private String mid;
    private String mymid;
    private int follow;
    private boolean blackList;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_personal_info;
    }

    @Override
    protected void initView() {
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        mid = bundle.getString("mid");
        spUtils = new SPUtils(this, SpConstant.USER_TOKEN);
        token = spUtils.getString(SpConstant.USER_TOKEN);
        mymid = spUtils.getLong(SpConstant.USER_MID) + "";
        getOtherUserInfoPresenter = new GetOtherUserInfoPresenter(new UserInfoCall());
        followUserPresenter = new FollowUserPresenter(new FollowUserCall());
        operateBLPresenter = new OperateBLPresenter(new OperaBlCall());
        fragments.add(new MySendBillFragment(mid));
        fragments.add(new MyDynamicFragment(mid));
        viewpager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return title[position];
            }
        });
        tabLayout.setViewPager(viewpager, title, this, fragments);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getOtherUserInfoPresenter.reqeust(mid, mymid);
    }

    @OnClick(R.id.ll_edit)
    public void editUserInfo() {
        if (TextUtils.isEmpty(token)) {
            intent(LoginActivity.class);
            return;
        }
        intent(EditInfoActivity.class);
    }

    @OnClick(R.id.iv_back)
    public void back() {
        finish();
    }

    @OnClick(R.id.iv_other)
    public void other() {
        showDialog();
    }

    @OnClick(R.id.ll_no_follow)
    public void follow() {
        followUserPresenter.reqeust(1, "Bearer " + token, mid);
    }

    @OnClick(R.id.ll_followed)
    public void cancelFollow() {
        followUserPresenter.reqeust(0, "Bearer " + token, mid);
    }

    boolean isFollower = false;

    private void showDialog() {
        View dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_report, null);
        BottomDialog bottomDialog = new BottomDialog(this);
        bottomDialog.setContentView(dialogView);
        bottomDialog.setCanceledOnTouchOutside(true);
        bottomDialog.show();
        TextView tv_cancel = dialogView.findViewById(R.id.tv_cancel);
        TextView tv_lahei = dialogView.findViewById(R.id.tv_lahei);
        LinearLayout ll_copy = dialogView.findViewById(R.id.ll_copy);
        LinearLayout ll_jubao = dialogView.findViewById(R.id.ll_jubao);
        LinearLayout ll_lahei = dialogView.findViewById(R.id.ll_lahei);
        tv_cancel.setOnClickListener(v -> bottomDialog.dismiss());
        if (!blackList) {
            tv_lahei.setText("拉黑");
        } else {
            tv_lahei.setText("取消拉黑");
        }
        ll_copy.setOnClickListener(v -> {
            ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
            ClipData clip = ClipData.newPlainText("simple text", "wwww.qudan.com");
            clipboard.setPrimaryClip(clip);
            UIUtils.showToastSafe("链接已复制到剪贴板");
        });
        ll_jubao.setOnClickListener(v -> {
            Bundle bundle = new Bundle();
            bundle.putString("mid",mid);
            intent(ReportActivity.class,bundle);
            bottomDialog.dismiss();
        });
        ll_lahei.setOnClickListener(v -> {
            if (blackList) {
                operateBLPresenter.reqeust("Bearer " + token, Long.parseLong(mid), blackList ? 0 : 1);
            } else {
                showOperBlDialog();
            }
            bottomDialog.dismiss();
        });
    }

    private void showOperBlDialog() {
        View dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_center_view, null);
        CenterDialog bottomDialog = new CenterDialog(this);
        bottomDialog.setContentView(dialogView);
        bottomDialog.setCanceledOnTouchOutside(true);
        bottomDialog.show();

        TextView tv_cancel = dialogView.findViewById(R.id.tv_cancel);
        TextView tv_sure = dialogView.findViewById(R.id.tv_sure);
        TextView tv_title = dialogView.findViewById(R.id.tv_title);
        tv_title.setText("拉黑后，你将不再收到对方消息，并且你们互相看不到对方的发单和动态信息");
        tv_cancel.setOnClickListener(v -> bottomDialog.dismiss());
        tv_sure.setOnClickListener(v -> {
            operateBLPresenter.reqeust("Bearer " + token, Long.parseLong(mid), blackList ? 0 : 1);
            bottomDialog.dismiss();
        });
    }

    class UserInfoCall implements DataCall<User> {

        @Override
        public void success(User data, Object... args) {
            tv_nickname.setText(data.getNickName());
            tv_id.setText("ID:" + data.getAutoId());
            tv_favorites.setText(data.getFavorites() + "");
            tv_follow.setText(data.getFollower() + "");
            blackList = data.isBlackList();
            if (mymid.equals(mid)) {
                ll_no_follow.setVisibility(View.GONE);
                ll_followed.setVisibility(View.GONE);
                iv_other.setVisibility(View.GONE);
                ll_edit.setVisibility(View.VISIBLE);
            } else {
                isFollower = data.isFollow();
                if (isFollower) {
                    ll_followed.setVisibility(View.VISIBLE);
                    ll_no_follow.setVisibility(View.GONE);
                } else {
                    ll_followed.setVisibility(View.GONE);
                    ll_no_follow.setVisibility(View.VISIBLE);
                }
                ll_edit.setVisibility(View.GONE);
                iv_other.setVisibility(View.VISIBLE);
            }
            Glide.with(PersonalInfoActivity.this).load(data.getImgUrl()).apply(RequestOptions.bitmapTransform(new CircleCrop())).into(iv_head);
            LevelImage.setLevelImage(data.getServiceLevel(), iv_level);
            if (TextUtils.isEmpty(data.getSign())) {
                sign.setText("这家伙很神秘，没有输入个人简介...");
            } else {
                sign.setText(data.getSign());
            }
            if (data.isIdentity()) {
                tv_sm.setText("实名已认证");
//                ll_sm.setBackgroundResource(R.drawable.shape_phone_bg_gray);
                iv_sm.setBackgroundResource(R.mipmap.sm_one);
                tv_sm.setTextColor(getResources().getColor(R.color.orange_home));
            } else {
                tv_sm.setText("实名未认证");
//                ll_sm.setBackgroundResource(R.drawable.shape_text_bg_gray);
                iv_sm.setBackgroundResource(R.mipmap.me_shiming);
                tv_sm.setTextColor(getResources().getColor(R.color.color_9C9DA1));
            }
            if (!TextUtils.isEmpty(data.getMobile())) {
                tv_phone.setText("手机已认证");
//                ll_phone.setBackgroundResource(R.drawable.shape_phone_bg_gray);
                iv_phone.setBackgroundResource(R.mipmap.me_phone);
                tv_phone.setTextColor(getResources().getColor(R.color.orange_home));
            } else {
                tv_phone.setText("手机未认证");
//                ll_phone.setBackgroundResource(R.drawable.shape_text_bg_gray);
                iv_phone.setBackgroundResource(R.mipmap.me_phone_two);
                tv_phone.setTextColor(getResources().getColor(R.color.color_9C9DA1));
            }

        }

        @Override
        public void fail(ApiException e, Object... args) {

        }
    }

    class FollowUserCall implements DataCall<String> {

        @Override
        public void success(String data, Object... args) {
            //还需要刷新处理
            getOtherUserInfoPresenter.reqeust(mid, mymid);
        }

        @Override
        public void fail(ApiException e, Object... args) {
            EventBus.getDefault().post(RefreshResult.getInstance(true, true));
        }
    }

    class OperaBlCall implements DataCall<String> {

        @Override
        public void success(String data, Object... args) {
            if (blackList) {
                UIUtils.showToastSafe("已取消黑名单");
            } else {
                UIUtils.showToastSafe("已加入黑名单");
            }
            //还需要刷新处理
            getOtherUserInfoPresenter.reqeust(mid, mymid);
        }

        @Override
        public void fail(ApiException e, Object... args) {
            EventBus.getDefault().post(RefreshResult.getInstance(true, true));
        }
    }

    @Override
    protected void destoryData() {

    }
}
